package com.smart.custom;

import com.fr.data.AbstractTableData;
import com.fr.general.data.TableDataException;
import com.fr.stable.ParameterProvider;
import com.fr.stable.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 集成fr report api  实现 通用取数类
 *
 *
 */
public class DynamicReportData extends AbstractTableData {
    //取数接口
    private DynamicQueryData Idynamicquery;
    //列名
    private String[] columnName;
//    private int colNum;
    //返回结果集
    private List<Object[]> rowData = null;



    /**
     * 返回列数
     * @return
     * @throws TableDataException
     */
    @Override
    public int getColumnCount() throws TableDataException {
        if(Idynamicquery==null) {
            String className = (String) this.parameters[0].getValue();
//          String tableName = ((ParameterProvider) (this.parameters
            System.out.println("=====" + className);
            if (!StringUtils.isEmpty(className)) {
                try {
//            Class queryData = Class.forName("com.smart.custom.DyeProProcessQueryData");
                    Class queryData = Class.forName(className);
                    Idynamicquery = (DynamicQueryData) queryData.newInstance();
                    Idynamicquery.initFeilds();
                    if (Idynamicquery.paras.size()==0) {
                        for (int i = 0; i < this.parameters.length; i++) {
                            Idynamicquery.paras.put( String.valueOf(i), this.parameters[i].getValue());
                        }
                    }
                    System.out.println("=====" + Idynamicquery.feildsHeard);
                    ArrayList field = Idynamicquery.feildsHeard;
                    columnName=new String[field.size()];
                    for (int i = 0; i < field.size(); i++) {
                        columnName[i] = (String) field.get(i);
                    }

                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InstantiationException e) {
                    e.printStackTrace();
                }
            }
        }
        return columnName.length;
    }
    /**
     * 返回列名
     * @param i
     * @return
     * @throws TableDataException
     */
    @Override
    public String getColumnName(int i) throws TableDataException {
        return  columnName[i];
    }
    /**
     * 进行一次查询，返回行数
     * @return
     * @throws TableDataException
     */
    @Override
    public int getRowCount() throws TableDataException {
        queryData();
        return rowData.size();
    }
    /**
     * 返回对应行列数的值
     * @param rowIndex
     * @param colIndex
     * @return
     */
    @Override
    public Object getValueAt(int rowIndex, int colIndex) {
        if (colIndex >=columnName.length ) {
            return null;
        }
        return ((Object[]) rowData.get(rowIndex))[colIndex];
    }

    /**
     *实现取数部分，通过前台的类参数，反射调用对应取数逻辑
     */
    private void queryData() {
        if(rowData!=null){
            return;

        }
        String className= (String) this.parameters[0].getValue();
//        System.out.println(className);
        rowData= Idynamicquery.queryData();


    }
}
